﻿var $j = jQuery.noConflict();



$j(document).ready(function(e) {
	var base_path = "http://localhost/wm/";
	// Campaign - begin
	//// Form
	if($('#edit-cpactivation').val() == 'MA')
		$('#edit-cpactivedate-wrapper').hide();
	$('#edit-cpactivation').change(function(){
		if($(this).val() == 'AU')
			$('#edit-cpactivedate-wrapper').show();
		else $('#edit-cpactivedate-wrapper').hide();
	});
		
	if($('#edit-cpstatus').val() != 'IN')
		$('.fieldset_cpactivation').hide();
	$('#edit-cpstatus').change(function(){
		if($(this).val() == 'IN')
			$('.fieldset_cpactivation').show();
		else $('.fieldset_cpactivation').hide();
	});
	
	var activedate = $j('#edit-cpactivedate').val();
	$j('#edit-cpactivedate').datepicker();
	$j('#edit-cpactivedate').datepicker("option", "dateFormat","dd/mm/yy");
	if(activedate!="")
		$j('#edit-cpactivedate').val(activedate);
	
	// Control Link
	$('a.cpactive').click(function(){
		if($(this).attr('status') == 'IN'){
			$.get(base_path + 'campaign/active/'+this.id, null, getResult);
			
			$(this).attr('status','AC');
			$(this).html('Deactivate');
			$(this).parent().prev().html('AC');
			$(this).parent().next().next().children('a.cpremove').attr('status','AC');
			$(this).parent().next().next().children('a.cpremove').html('');
		}else
		if($(this).attr('status') == 'AC'){
			$.get(base_path + 'campaign/inactive/'+this.id, null, getResult);
			$(this).attr('status','IN')
			$(this).html('Activate');
			$(this).parent().prev().html('IN');
			if($(this).attr('progess') == "0")
				$(this).parent().next().next().children('a.cpremove').html('Remove');
			$(this).parent().next().next().children('a.cpremove').attr('status','IN');
		}
		return false;
	});
	
	$('a.cpremove').click(function(){
		if(confirm('Are you sure you want to delete?'))
		if($(this).attr('status') == 'IN'){
			$.get(base_path + 'campaign/remove/'+this.id, null, getResult);
			$(this).parent().parent().html('');
		}
		return false;
	});
	
	// $('a.cpprogress').each(function(){
		// /*$.get(base_path + 'campaign/progress/'+this.id, null, getProgressResult);*/
		// return false;
	// });
	var getProgressResult = function(response) {
		var result = Drupal.parseJson(response);
		alert(result.numslave);
	}
	
	// Campaign - end

	// Slave Site - begin
	//// Control Link
	$('a.slfinish').click(function(){
		if($(this).attr('status') != 'finish'){
			$.get(base_path + 'slavesite/finish/'+this.id, null, getResult);
			$(this).attr('status','testing');
			$(this).html('');
			$(this).parent().prev().prev().prev().html('testing');
			$(this).parent().prev().prev().children('a.slpattern').html('Pattern');
		}
		return false;
	});
	
	$('a.slremove').click(function(){
		if(confirm('Are you sure you want to delete?'))
		if($(this).attr('status') != 'removed'){
			$.get(base_path + 'slavesite/remove/'+this.id, null, getResult);
			$(this).attr('status','removed');
			$(this).html('');
			$(this).parent().parent().html('');
			// lqan fix #12
			var myWindow = window.open('temp/sl.php?id='+this.id,'Popupwindow','width=400,height=400, height=400,resizable=false,scrollbars=no');
			if (!myWindow.opener) myWindow.opener = self;
		}
		return false;
	});
	// Slave Site - end



	// Site Pattern - begin
	//// Control Link
	$('a.spremove').click(function(){
		if(confirm('Are you sure you want to delete?')){
			$.get(base_path + 'sitepattern/remove/'+this.id, null, getResult);
			$(this).parent().parent().html('');
		}
		return false;
	});
	// Site Pattern - end

	// Site Pattern - begin
	//// Control Link
	
	$('#type1').hide();
	$('#type2').hide();
	// lqan 6/9 : #30
	$('#type3').hide();
	if($("input[name='type']").filter('[value=1]').attr('checked')==true){
		$('#type1').show();
	}
	if($("input[name='type']").filter('[value=2]').attr('checked')==true){
		$('#type2').show();
	}
	if($("input[name='type']").filter('[value=3]').attr('checked')==true){
		$('#type3').show();
	}
	
	if($("#edit-replacement").text()=="")
	{
		//$("#edit-replacement").hide();
		// lqan 7/11 : disable when beginning
		$("#edit-replacement").attr("disabled", "disabled");
		$("input[name='nullDOM']").attr('checked', true);
	}
	else
	{
		//$("#edit-replacement").show();
		// lqan 7/11 : enable in jquery
		$("#edit-replacement").removeAttr("disabled");
		$("input[name='nullDOM']").attr('checked', false);
	}
	$("input[name='nullDOM']").change(function(){
		if($("input[name='nullDOM']").attr("checked")==true)
		{
			//$("#edit-replacement").hide();
			// lqan 7/11 : disable when beginning in jquery
			$("#edit-replacement").val("");
			$("#edit-replacement").attr("disabled", "disabled");
		}
		else 
			//$("#edit-replacement").show();
			// lqan 7/11 : enable ịn jquery
			$("#edit-replacement").removeAttr("disabled");
	});
	
	$("input[name='type']").change(function(){
		if ($("input[name='type']:checked").val() == '0') {
			$('#type1').hide();
			$('#type2').hide();
			// lqan 6/9 : #30
			$('#type3').hide();			
		}
		if ($("input[name='type']:checked").val() == '1') {
			$('#type1').show();
			$('#type2').hide();
			// lqan 6/9 : #30
			$('#type3').hide();			
		}
		if ($("input[name='type']:checked").val() == '2') {
			$('#type1').hide();
			$('#type2').show();
			// lqan 6/9 : #30
			$('#type3').hide();
		}
		// lqan 6/9 : #30
		if ($("input[name='type']:checked").val() == '3') {
			$('#type1').hide();
			$('#type2').hide();
			// lqan 6/9 : #30
			$('#type3').show();
		}
		// lqan end
	});
	$('a.spremove').click(function(){
		if(confirm('Are you sure you want to delete?')){
			$.get(base_path + 'sitepattern/remove/'+this.id, null, getResult);
			$(this).parent().parent().html('');
		}
		return false;
	});
	
	// Site Pattern - end

	// Tester Board - begin
	//// Control Link
	$('a.ttfailed').click(function(){
		$.get(base_path + 'testerboard/failed/'+this.id, null, getResult);
		$(this).parent().prev().prev().html('failed');
		$(this).parent().next().children('a.ttpass').html('Pass');
		$(this).html('');
		// lqan fix #12
		var myWindow = window.open('temp/sl.php?id='+this.id,'Popupwindow','width=400,height=400, height=400,resizable=false,scrollbars=no');
		if (!myWindow.opener) myWindow.opener = self;
		return false;
	});
	$('a.ttpass').click(function(){
		$.get(base_path + 'testerboard/pass/'+this.id, null, getResult);
		$(this).parent().prev().prev().prev().html('pass');
		$(this).parent().prev().children('a.ttfailed').html('Fail');
		$(this).html('');
		return false;
	});
	// Tester Board - end
	
	
	// Deployment - begin
	//// Control Link
	$('input.dpcbcountry').change(function(){
		$.get(base_path + 'deployment/country/'+this.id+'/'+$(this).attr('country'), null, getResult);
		return false;
	});
	// Deployment - end
	
	// Confirm - begin
	//// Control Link
	$('a.cfreject').click(function(){
		$.get(base_path + 'confirm/reject/'+this.id, null, getResult);
		$(this).parent().parent().html('');
		return false;
	});
	$('a.cfapprove').click(function(){
		$.get(base_path + 'confirm/approve/' + this.id, null, getResult);
		$(this).parent().prev().prev().prev().html('approved');
		$(this).html('');
		return false;
	});
	// Confirm - end
	
	// lqan 9/12 
	$('a.tttest').click(function(){
		$.get(base_path + 'testerboard/test/' + this.id, null, getResult);
		
	 	var myWindow = window.open('testboard/?testprofile=?'+this.id,'Popupwindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=800,height=500,left=50,top=50');
		if (!myWindow.opener) myWindow.opener = self;
		return false;  
	});
	// Test - end
	
	
	
	$('a.dpdeploy').click(function(){
		
		//alert(document.getElementById(this.id).value);
		var multipleValues = $("#"+this.id).val() || [];
		var str = multipleValues.join("-");
		$.get(base_path + 'deployment/country/' + this.id + "/" + str, null, getResult);
		//alert(base_path + 'deployment/country/' + this.id + "/" + str);

	});
	
	
	
	var getResult = function(response) {
		// var result = Drupal.parseJson(response);
		// alert(result.numslave);
		// alert(result.country);
	}
	
});

// Drupal.behaviors.sitecloner = function(context) {
	// $('a.cpactive').bind('click', function(){
		// alert('vao');
		// if(this.status == 'IN'){
			// $.get('campaign/active/'+this.id, null, getResult);
			// this.status='AC';
		// }
		// if(this.status == 'AC'){
			// $.get('campaign/inactive/'+this.id, null, getResult);
			// this.status='IN';
		// }
		// return false;
	// });
// }
